package com.heima.behavior.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.dto.LikesBehaviorDto;
import com.heima.behavior.entity.ApLikesBehavior;
import com.heima.behavior.mapper.ApLikesBehaviorMapper;
import com.heima.behavior.service.IApLikesBehaviorService;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * APP点赞行为表 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-01-22
 */
@Service
public class ApLikesBehaviorServiceImpl extends ServiceImpl<ApLikesBehaviorMapper, ApLikesBehavior> implements IApLikesBehaviorService {

    @Override
    public ResponseResult like(LikesBehaviorDto dto) {
        //查询是否已经点赞了--查找符合条件的点赞数据
        LambdaQueryWrapper<ApLikesBehavior> qw = new LambdaQueryWrapper<>();
        qw.eq(ApLikesBehavior::getArticleId, dto.getArticleId());
        qw.eq(ApLikesBehavior::getEntryId, dto.getEntryId());
        ApLikesBehavior one = this.getOne(qw);
        //判断
        if (one == null) {
            //未点赞--保存该点赞操作
            ApLikesBehavior apLikesBehavior = new ApLikesBehavior();
            BeanUtils.copyProperties(dto, apLikesBehavior);
            this.save(apLikesBehavior);
        } else{
            //已点赞--取消
            if (one.getOperation().equals(0)) {
                one.setOperation(1);
                this.updateById(one);
            } else {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
            }
        }
        return ResponseResult.okResult();
    }
}
